from django.shortcuts import render
from rest_framework.generics import CreateAPIView,ListAPIView
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from orders.models import OrdersModel
from orders.OrdersSerializer import OrderModelSerializers,OrderListModelSerializer
from rest_framework.pagination import PageNumberPagination
# Create your views here.
# 生成订单
class OrderCreateAPIViews(CreateAPIView):
    permission_classes = [IsAuthenticated]
    queryset = OrdersModel.objects.all()
    serializer_class = OrderModelSerializers

# 获取订单状态列表  选项
class OrderPayStatusChoicesApiView(APIView):
    def get(self,request):
        return Response(OrdersModel.STATUS_CHOICES)
# 获取分页
class OrderListPageNumberPagination(PageNumberPagination):
    page_size = 5
    max_page_size = 10
    page_query_param = 'page'
    page_size_query_param = 'size'

# 获取当前订单列表
class OrderListApiView(ListAPIView):
    permission_classes = [IsAuthenticated]
    serializer_class = OrderListModelSerializer
    pagination_class = OrderListPageNumberPagination
    def get_queryset(self):
        user = self.request.user
        # 根据当前用户，查询全部订单
        query_set = OrdersModel.objects.filter(user=user,is_deleted=False,is_show=True)
        order_status = int(self.request.query_params.get('status',-1))
        # 判断4次
        # 获取数据库存储的订单状态码
        status_list = [item[0] for item in OrdersModel.STATUS_CHOICES]
        if order_status in status_list:
            query_set = query_set.filter(order_status=order_status)
        return query_set.order_by('-id')














